草庐IT

HTML、CSS : Display HTML code with `` , `<pre>` 或 `code` ?</h1><span><a href="/alltag/">全部标签</a></span></div><div class="item"><h2><a href="/v/6vpsd2/">ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式</a></h2><div class="post"> 我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h</div><div class="tag"><a href="/t/ruby/">ruby</a><a href="/t/HTML/">HTML</a><a href="/t/code/">code</a><a href="/t/gt/">gt</a><a href="/t/section/">section</a><a href="/t/html-parsing/">html-parsing</a><a href="/t/nokogiri/">nokogiri</a><a href="/t/beautifulsoup/">beautifulsoup</a><a href="/t/hpricot/">hpricot</a></div></div><div class="item"><h2><a href="/v/tifqh6/">ruby - capybara field.has_css?匹配器</a></h2><div class="post"> 我在MiniTest::Spec和Capybara中使用以下规范:find_field('Email').must_have_css('[autofocus]')检查名为“电子邮件”的字段是否具有autofocus属性。doc说如下:has_css?(path,options={})ChecksifagivenCSSselectorisonthepageorcurrentnode.据我了解,字段“Email”是一个节点,因此调用must_have_css绝对有效!我做错了什么? 最佳答案 通过JonasNicklas得到了答案:No</div><div class="tag"><a href="/t/配器/">配器</a><a href="/t/capybara/">capybara</a><a href="/t/code/">code</a><a href="/t/section/">section</a><a href="/t/autofocus/">autofocus</a><a href="/t/ruby/">ruby</a><a href="/t/ruby-on-rails-3/">ruby-on-rails-3</a><a href="/t/minitest/">minitest</a></div></div><div class="item"><h2><a href="/v/bs1kp7/">ruby-on-rails - 如何从 format.xml 中删除 &lt;hash&gt;&lt;/hash&gt;</a></h2><div class="post"> 我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为</div><div class="tag"><a href="/t/amp/">amp</a><a href="/t/hash/">hash</a><a href="/t/code/">code</a><a href="/t/gt/">gt</a><a href="/t/lt/">lt</a><a href="/t/ruby-on-rails/">ruby-on-rails</a><a href="/t/ruby/">ruby</a><a href="/t/ruby-on-rails-3.1/">ruby-on-rails-3.1</a></div></div><div class="item"><h2><a href="/v/9l5bf8/">ruby-on-rails - Rails HTML 请求渲染 JSON</a></h2><div class="post"> 在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这</div><div class="tag"><a href="/t/ruby-on-rails/">ruby-on-rails</a><a href="/t/Rails/">Rails</a><a href="/t/code/">code</a><a href="/t/section/">section</a><a href="/t/pre/">pre</a><a href="/t/ruby/">ruby</a><a href="/t/json/">json</a></div></div><div class="item"><h2><a href="/v/qbidku/">ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB?</a></h2><div class="post"> 所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择</div><div class="tag"><a href="/t/ruby-on-rails/">ruby-on-rails</a><a href="/t/突出/">突出</a><a href="/t/section/">section</a><a href="/t/code/">code</a><a href="/t/Sublime/">Sublime</a><a href="/t/ruby/">ruby</a><a href="/t/editor/">editor</a><a href="/t/sublimetext2/">sublimetext2</a><a href="/t/sublimetext3/">sublimetext3</a></div></div><div class="item"><h2><a href="/v/r22toi/">ruby-on-rails - rspec should have_select (&#39;cars&#39; , :options =&gt; [&#39;volvo&#39; , &#39;saab&#39; ] 不工作</a></h2><div class="post"> 关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request</div><div class="tag"><a href="/t/amp/">amp</a><a href="/t/39/">39</a><a href="/t/section/">section</a><a href="/t/option/">option</a><a href="/t/class/">class</a><a href="/t/ruby-on-rails/">ruby-on-rails</a><a href="/t/ruby/">ruby</a><a href="/t/rspec/">rspec</a></div></div><div class="item"><h2><a href="/v/44nkv5/">ruby-on-rails - Nokogiri:使用 XPath 搜索 &lt;div&gt;</a></h2><div class="post"> 我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll</div><div class="tag"><a href="/t/ruby-on-rails/">ruby-on-rails</a><a href="/t/amp/">amp</a><a href="/t/lt/">lt</a><a href="/t/gt/">gt</a><a href="/t/39/">39</a><a href="/t/ruby/">ruby</a><a href="/t/regex/">regex</a><a href="/t/xpath/">xpath</a><a href="/t/nokogiri/">nokogiri</a></div></div><div class="item"><h2><a href="/v/sp1hmq/">ruby-on-rails - Ruby url 到 html 链接转换</a></h2><div class="post"> 我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.</div><div class="tag"><a href="/t/ruby-on-rails/">ruby-on-rails</a><a href="/t/rails/">rails</a><a href="/t/section/">section</a><a href="/t/rubyonrails/">rubyonrails</a><a href="/t/loudthinking/">loudthinking</a><a href="/t/ruby/">ruby</a><a href="/t/regex/">regex</a></div></div><div class="item"><h2><a href="/v/3v0071/">ruby-on-rails - capybara ::ElementNotFound:无法找到 xpath &#34;/html&#34;</a></h2><div class="post"> 我正在学习http://ruby.railstutorial.org/chapters/static-pages上的RubyonRails教程并遇到以下错误StaticPagesHomepageshouldhavethecontent'SampleApp'Failure/Error:page.shouldhave_content('SampleApp')Capybara::ElementNotFound:Unabletofindxpath"/html"#(eval):2:in`text'#./spec/requests/static_pages_spec.rb:7:in`(root)'</div><div class="tag"><a href="/t/ElementNotFound/">ElementNotFound</a><a href="/t/ruby-on-rails/">ruby-on-rails</a><a href="/t/39/">39</a><a href="/t/section/">section</a><a href="/t/34/">34</a><a href="/t/ruby/">ruby</a><a href="/t/rspec/">rspec</a><a href="/t/capybara/">capybara</a><a href="/t/gemfile/">gemfile</a></div></div><div class="item"><h2><a href="/v/8u9i1d/">css - 用 watir 检查标签类?</a></h2><div class="post"> 我有一个div,它根据表单是否正确提交而改变。我想知道是否可以检查类的特定元素?开始元素看起来像这样。如果输入不正确,添加错误类。 最佳答案 试试这个:browser.div(:id=>"myerrortest").class_name更多信息:http://watir.github.com/watir-webdriver/doc/Watir/HTMLElement.html#class_name-instance_method另一种选择是只查看具有您期望的类的div是否存在browser.div((:id=>"myerrortes</div><div class="tag"><a href="/t/watir/">watir</a><a href="/t/css/">css</a><a href="/t/section/">section</a><a href="/t/code/">code</a><a href="/t/myerrortest/">myerrortest</a><a href="/t/ruby/">ruby</a><a href="/t/class/">class</a></div></div> <div class="page"><span>1</span><a href="/t/HTML、CSS : Display HTML code with `<xmp>` , `<pre>` 或 `code` ?/2/">2</a><a href="/t/HTML、CSS : Display HTML code with `<xmp>` , `<pre>` 或 `code` ?/3/">3</a><a href="/t/HTML、CSS : Display HTML code with `<xmp>` , `<pre>` 或 `code` ?/4/">4</a></div> </div> </div> <div id="foot"> <div class="ft-nav"> <a href="/">Home</a> <p>Powered By IT.CAOLU.XIN</p> <p>页面耗时:0.1515s</p> </div> </div> </body> </html> <script>hljs.highlightAll();</script>